Rangers struck late to beat St Johnstone and move to within six points of second-placed Aberdeen in the Scottish Premiership.